﻿Imports System.Data.OleDb

Public Class CommentsManagement
    Inherits ConnectionClass

    Public Sub InsertComment(ByVal quizId As Int32, ByVal userId As Int32, ByVal CommentContent As String, ByVal CommentDate As DateTime, ByVal CommentVisible As Boolean)
        Try

        
        myConnection.Open()
        Dim SqlString As String = "INSERT INTO Comments(QuizId,UserId, CommentContent,CommentDate,CommentVisible) VALUES(@f1,@f2,@f3,@f4,@f5)"
        Dim newCmd As OleDbCommand = New OleDbCommand(SqlString, myConnection)
        newCmd.CommandType = CommandType.Text
        newCmd.Parameters.AddWithValue("@f1", quizId)
        newCmd.Parameters.AddWithValue("@f2", userId)
        newCmd.Parameters.AddWithValue("@f3", CommentContent)
        newCmd.Parameters.AddWithValue("@f4", CommentDate)
        newCmd.Parameters.AddWithValue("@f5", CommentVisible)
        newCmd.ExecuteNonQuery()
            myConnection.Close()
        Catch ex As Exception

        End Try
    End Sub


    Public Function SelectCommentbyCommentID(ByVal commentId As Int32) As OleDbDataReader
        Try

        
        myConnection.Open()
        Dim SqlString As String = "Select * From Comments Where CommentId=@f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, myConnection)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", commentId)
        Dim dr As OleDbDataReader = cmd.ExecuteReader()

        Return dr
        myConnection.Close()

        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Function SelectCommentVisibleDesc() As OleDbDataReader

        Try

            myConnection.Open()
            Dim SqlString As String = "SELECT CommentId, QuizId, UserId, CommentContent, CommentDate, CommentVisible FROM Comments WHERE (CommentVisible = True) ORDER BY CommentId DESC"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, myConnection)
            cmd.CommandType = CommandType.Text

            Dim dr As OleDbDataReader = cmd.ExecuteReader()

            Return dr
            myConnection.Close()

        Catch ex As Exception
            Return Nothing
        End Try
    End Function


    Public Sub EditApproveCommentByCommentId(ByVal commentID As Int32)
        Try

            myConnection.Open()
            Dim SqlString As String = "UPDATE   Comments Set CommentVisible =@f1 where CommentID = @f2"
            Dim newCmd As OleDbCommand = New OleDbCommand(SqlString, myConnection)
            newCmd.CommandType = CommandType.Text
            newCmd.Parameters.AddWithValue("@f1", True)
            newCmd.Parameters.AddWithValue("@f2", commentID)
            newCmd.ExecuteNonQuery()
            myConnection.Close()

        Catch ex As Exception

        End Try
    End Sub

    Public Sub EditCommentByCommentId(ByVal commentID As Int32, ByVal commentContent As String)
        Try


            myConnection.Open()
            Dim SqlString As String = "UPDATE   Comments Set CommentContent =@f1 where CommentID = @f2"
            Dim newCmd As OleDbCommand = New OleDbCommand(SqlString, myConnection)
            newCmd.CommandType = CommandType.Text
            newCmd.Parameters.AddWithValue("@f1", commentContent)
            newCmd.Parameters.AddWithValue("@f2", commentID)
            newCmd.ExecuteNonQuery()
            myConnection.Close()
        Catch ex As Exception

        End Try
    End Sub

    Public Sub DeleteCommentByCommentId(ByVal commentID As Int32)
        Try

        
        myConnection.Open()
        Dim SqlString As String = "DELETE FROM Comments WHERE(CommentId = @f1)"
        Dim newCmd As OleDbCommand = New OleDbCommand(SqlString, myConnection)
        newCmd.CommandType = CommandType.Text
        newCmd.Parameters.AddWithValue("@f1", commentID)
        newCmd.ExecuteNonQuery()
            myConnection.Close()

        Catch ex As Exception

        End Try
    End Sub

End Class
